{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>标题</th>\n",
       "      <th>建筑面积</th>\n",
       "      <th>总价</th>\n",
       "      <th>户型</th>\n",
       "      <th>朝向</th>\n",
       "      <th>物 业 费</th>\n",
       "      <th>环比上月</th>\n",
       "      <th>张贴日期</th>\n",
       "      <th>产权性质</th>\n",
       "      <th>装修</th>\n",
       "      <th>楼层</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>资深经纪上海人：稀缺3房，南北通透采光好，房东急需用钱！</td>\n",
       "      <td>123.50</td>\n",
       "      <td>760</td>\n",
       "      <td>3室2厅0厨2卫</td>\n",
       "      <td>南北</td>\n",
       "      <td>1.5元/平米・月</td>\n",
       "      <td>↑1.44%</td>\n",
       "      <td>西元2017年4月12日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>豪华装修</td>\n",
       "      <td>低层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>未来规划小上海，精装带地暖，养老，亲子，旅游度假一体化</td>\n",
       "      <td>86.00</td>\n",
       "      <td>76</td>\n",
       "      <td>2室2厅1厨1卫</td>\n",
       "      <td>南北</td>\n",
       "      <td>1.8元/・月</td>\n",
       "      <td>↑4.08%</td>\n",
       "      <td>西元2017年4月13日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>精装修</td>\n",
       "      <td>中层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>双高铁 不限贷 买一层送一层 送大露台 看房专车接送</td>\n",
       "      <td>90.00</td>\n",
       "      <td>80</td>\n",
       "      <td>3室2厅1厨2卫</td>\n",
       "      <td>南北</td>\n",
       "      <td>暂无资料</td>\n",
       "      <td>↓0.01%</td>\n",
       "      <td>西元2017年4月14日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>精装修</td>\n",
       "      <td>高层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>坐拥嘉兴海宁双轨 享受市区配套 现房值得拥有 可落户</td>\n",
       "      <td>90.00</td>\n",
       "      <td>80</td>\n",
       "      <td>2室1厅1厨1卫</td>\n",
       "      <td>南北</td>\n",
       "      <td>暂无资料</td>\n",
       "      <td>↓0.01%</td>\n",
       "      <td>西元2017年4月15日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>简装修</td>\n",
       "      <td>中层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>对口明强校区，精装两房税费少，位置安静，媲美园艺新村，随时看</td>\n",
       "      <td>66.61</td>\n",
       "      <td>285</td>\n",
       "      <td>2室1厅1厨1卫</td>\n",
       "      <td>南</td>\n",
       "      <td>0.8元/平米・月</td>\n",
       "      <td>↓2.62%</td>\n",
       "      <td>西元2017年4月16日</td>\n",
       "      <td>个人产权</td>\n",
       "      <td>中装修</td>\n",
       "      <td>低层</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               标题    建筑面积   总价        户型  朝向      物 业 费  \\\n",
       "0    资深经纪上海人：稀缺3房，南北通透采光好，房东急需用钱！  123.50  760  3室2厅0厨2卫  南北  1.5元/平米・月   \n",
       "1     未来规划小上海，精装带地暖，养老，亲子，旅游度假一体化   86.00   76  2室2厅1厨1卫  南北    1.8元/・月   \n",
       "2      双高铁 不限贷 买一层送一层 送大露台 看房专车接送   90.00   80  3室2厅1厨2卫  南北       暂无资料   \n",
       "3      坐拥嘉兴海宁双轨 享受市区配套 现房值得拥有 可落户   90.00   80  2室1厅1厨1卫  南北       暂无资料   \n",
       "4  对口明强校区，精装两房税费少，位置安静，媲美园艺新村，随时看   66.61  285  2室1厅1厨1卫   南  0.8元/平米・月   \n",
       "\n",
       "     环比上月          张贴日期  产权性质    装修  楼层  \n",
       "0  ↑1.44%  西元2017年4月12日   商品房  豪华装修  低层  \n",
       "1  ↑4.08%  西元2017年4月13日   商品房   精装修  中层  \n",
       "2  ↓0.01%  西元2017年4月14日   商品房   精装修  高层  \n",
       "3  ↓0.01%  西元2017年4月15日   商品房   简装修  中层  \n",
       "4  ↓2.62%  西元2017年4月16日  个人产权   中装修  低层  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "df = pandas.read_excel('Data\\house_sample.xlsx')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>南</th>\n",
       "      <th>南北</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    南  南北\n",
       "0   0   1\n",
       "1   0   1\n",
       "2   0   1\n",
       "3   0   1\n",
       "4   1   0\n",
       "5   0   1\n",
       "6   1   0\n",
       "7   1   0\n",
       "8   1   0\n",
       "9   0   1\n",
       "10  0   1\n",
       "11  1   0\n",
       "12  0   1\n",
       "13  0   1\n",
       "14  0   1\n",
       "15  1   0\n",
       "16  1   0\n",
       "17  0   1\n",
       "18  1   0\n",
       "19  1   0\n",
       "20  0   1\n",
       "21  1   0\n",
       "22  1   0\n",
       "23  0   1\n",
       "25  0   1\n",
       "26  1   0\n",
       "27  1   0\n",
       "28  0   1\n",
       "29  1   0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pandas.get_dummies(df['朝向'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = pandas.concat([df, pandas.get_dummies(df['朝向'])], axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>标题</th>\n",
       "      <th>建筑面积</th>\n",
       "      <th>总价</th>\n",
       "      <th>户型</th>\n",
       "      <th>物 业 费</th>\n",
       "      <th>环比上月</th>\n",
       "      <th>张贴日期</th>\n",
       "      <th>产权性质</th>\n",
       "      <th>装修</th>\n",
       "      <th>楼层</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>资深经纪上海人：稀缺3房，南北通透采光好，房东急需用钱！</td>\n",
       "      <td>123.50</td>\n",
       "      <td>760</td>\n",
       "      <td>3室2厅0厨2卫</td>\n",
       "      <td>1.5元/平米・月</td>\n",
       "      <td>↑1.44%</td>\n",
       "      <td>西元2017年4月12日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>豪华装修</td>\n",
       "      <td>低层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>未来规划小上海，精装带地暖，养老，亲子，旅游度假一体化</td>\n",
       "      <td>86.00</td>\n",
       "      <td>76</td>\n",
       "      <td>2室2厅1厨1卫</td>\n",
       "      <td>1.8元/・月</td>\n",
       "      <td>↑4.08%</td>\n",
       "      <td>西元2017年4月13日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>精装修</td>\n",
       "      <td>中层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>双高铁 不限贷 买一层送一层 送大露台 看房专车接送</td>\n",
       "      <td>90.00</td>\n",
       "      <td>80</td>\n",
       "      <td>3室2厅1厨2卫</td>\n",
       "      <td>暂无资料</td>\n",
       "      <td>↓0.01%</td>\n",
       "      <td>西元2017年4月14日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>精装修</td>\n",
       "      <td>高层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>坐拥嘉兴海宁双轨 享受市区配套 现房值得拥有 可落户</td>\n",
       "      <td>90.00</td>\n",
       "      <td>80</td>\n",
       "      <td>2室1厅1厨1卫</td>\n",
       "      <td>暂无资料</td>\n",
       "      <td>↓0.01%</td>\n",
       "      <td>西元2017年4月15日</td>\n",
       "      <td>商品房</td>\n",
       "      <td>简装修</td>\n",
       "      <td>中层</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>对口明强校区，精装两房税费少，位置安静，媲美园艺新村，随时看</td>\n",
       "      <td>66.61</td>\n",
       "      <td>285</td>\n",
       "      <td>2室1厅1厨1卫</td>\n",
       "      <td>0.8元/平米・月</td>\n",
       "      <td>↓2.62%</td>\n",
       "      <td>西元2017年4月16日</td>\n",
       "      <td>个人产权</td>\n",
       "      <td>中装修</td>\n",
       "      <td>低层</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               标题    建筑面积   总价        户型      物 业 费    环比上月  \\\n",
       "0    资深经纪上海人：稀缺3房，南北通透采光好，房东急需用钱！  123.50  760  3室2厅0厨2卫  1.5元/平米・月  ↑1.44%   \n",
       "1     未来规划小上海，精装带地暖，养老，亲子，旅游度假一体化   86.00   76  2室2厅1厨1卫    1.8元/・月  ↑4.08%   \n",
       "2      双高铁 不限贷 买一层送一层 送大露台 看房专车接送   90.00   80  3室2厅1厨2卫       暂无资料  ↓0.01%   \n",
       "3      坐拥嘉兴海宁双轨 享受市区配套 现房值得拥有 可落户   90.00   80  2室1厅1厨1卫       暂无资料  ↓0.01%   \n",
       "4  对口明强校区，精装两房税费少，位置安静，媲美园艺新村，随时看   66.61  285  2室1厅1厨1卫  0.8元/平米・月  ↓2.62%   \n",
       "\n",
       "           张贴日期  产权性质    装修  楼层  \n",
       "0  西元2017年4月12日   商品房  豪华装修  低层  \n",
       "1  西元2017年4月13日   商品房   精装修  中层  \n",
       "2  西元2017年4月14日   商品房   精装修  高层  \n",
       "3  西元2017年4月15日   商品房   简装修  中层  \n",
       "4  西元2017年4月16日  个人产权   中装修  低层  "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.drop('朝向', axis = 1)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "?df.pivot_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>产权性质</th>\n",
       "      <th>个人产权</th>\n",
       "      <th>商品房</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张贴日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>西元2017年4月12日</th>\n",
       "      <td>0</td>\n",
       "      <td>760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西元2017年4月13日</th>\n",
       "      <td>0</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西元2017年4月14日</th>\n",
       "      <td>560</td>\n",
       "      <td>740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西元2017年4月15日</th>\n",
       "      <td>650</td>\n",
       "      <td>975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>西元2017年4月16日</th>\n",
       "      <td>285</td>\n",
       "      <td>1310</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "产权性质          个人产权   商品房\n",
       "张贴日期                    \n",
       "西元2017年4月12日     0   760\n",
       "西元2017年4月13日     0    76\n",
       "西元2017年4月14日   560   740\n",
       "西元2017年4月15日   650   975\n",
       "西元2017年4月16日   285  1310"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.pivot_table(index = '张贴日期', columns='产权性质', values='总价', aggfunc=sum, fill_value=0)\n",
    "df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>张贴日期</th>\n",
       "      <th>西元2017年4月12日</th>\n",
       "      <th>西元2017年4月13日</th>\n",
       "      <th>西元2017年4月14日</th>\n",
       "      <th>西元2017年4月15日</th>\n",
       "      <th>西元2017年4月16日</th>\n",
       "      <th>西元2017年4月17日</th>\n",
       "      <th>西元2017年4月18日</th>\n",
       "      <th>西元2017年4月19日</th>\n",
       "      <th>西元2017年4月20日</th>\n",
       "      <th>西元2017年4月21日</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>产权性质</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>个人产权</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>560</td>\n",
       "      <td>650</td>\n",
       "      <td>285</td>\n",
       "      <td>875</td>\n",
       "      <td>1384</td>\n",
       "      <td>298</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品房</th>\n",
       "      <td>760</td>\n",
       "      <td>76</td>\n",
       "      <td>740</td>\n",
       "      <td>975</td>\n",
       "      <td>1310</td>\n",
       "      <td>575</td>\n",
       "      <td>850</td>\n",
       "      <td>769</td>\n",
       "      <td>400</td>\n",
       "      <td>270</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "张贴日期  西元2017年4月12日  西元2017年4月13日  西元2017年4月14日  西元2017年4月15日  西元2017年4月16日  \\\n",
       "产权性质                                                                         \n",
       "个人产权             0             0           560           650           285   \n",
       "商品房            760            76           740           975          1310   \n",
       "\n",
       "张贴日期  西元2017年4月17日  西元2017年4月18日  西元2017年4月19日  西元2017年4月20日  西元2017年4月21日  \n",
       "产权性质                                                                        \n",
       "个人产权           875          1384           298             0             0  \n",
       "商品房            575           850           769           400           270  "
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = df.pivot_table(index = '产权性质', columns='张贴日期', values='总价', aggfunc=sum, fill_value=0)\n",
    "df3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>张贴日期</th>\n",
       "      <th>西元2017年4月12日</th>\n",
       "      <th>西元2017年4月13日</th>\n",
       "      <th>西元2017年4月14日</th>\n",
       "      <th>西元2017年4月15日</th>\n",
       "      <th>西元2017年4月16日</th>\n",
       "      <th>西元2017年4月17日</th>\n",
       "      <th>西元2017年4月18日</th>\n",
       "      <th>西元2017年4月19日</th>\n",
       "      <th>西元2017年4月20日</th>\n",
       "      <th>西元2017年4月21日</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>产权性质</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>个人产权</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>560</td>\n",
       "      <td>650</td>\n",
       "      <td>285</td>\n",
       "      <td>875</td>\n",
       "      <td>1384</td>\n",
       "      <td>298</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品房</th>\n",
       "      <td>760</td>\n",
       "      <td>76</td>\n",
       "      <td>740</td>\n",
       "      <td>975</td>\n",
       "      <td>1310</td>\n",
       "      <td>575</td>\n",
       "      <td>850</td>\n",
       "      <td>769</td>\n",
       "      <td>400</td>\n",
       "      <td>270</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "张贴日期  西元2017年4月12日  西元2017年4月13日  西元2017年4月14日  西元2017年4月15日  西元2017年4月16日  \\\n",
       "产权性质                                                                         \n",
       "个人产权             0             0           560           650           285   \n",
       "商品房            760            76           740           975          1310   \n",
       "\n",
       "张贴日期  西元2017年4月17日  西元2017年4月18日  西元2017年4月19日  西元2017年4月20日  西元2017年4月21日  \n",
       "产权性质                                                                        \n",
       "个人产权           875          1384           298             0             0  \n",
       "商品房            575           850           769           400           270  "
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_multi_idx = df.pivot_table(index = ['装修','楼层'], columns='张贴日期', values='总价', aggfunc=sum, fill_value=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>张贴日期</th>\n",
       "      <th colspan=\"3\" halign=\"left\">西元2017年4月12日</th>\n",
       "      <th colspan=\"3\" halign=\"left\">西元2017年4月13日</th>\n",
       "      <th colspan=\"3\" halign=\"left\">西元2017年4月14日</th>\n",
       "      <th>西元2017年4月15日</th>\n",
       "      <th>...</th>\n",
       "      <th>西元2017年4月18日</th>\n",
       "      <th colspan=\"3\" halign=\"left\">西元2017年4月19日</th>\n",
       "      <th colspan=\"3\" halign=\"left\">西元2017年4月20日</th>\n",
       "      <th colspan=\"3\" halign=\"left\">西元2017年4月21日</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>楼层</th>\n",
       "      <th>中层</th>\n",
       "      <th>低层</th>\n",
       "      <th>高层</th>\n",
       "      <th>中层</th>\n",
       "      <th>低层</th>\n",
       "      <th>高层</th>\n",
       "      <th>中层</th>\n",
       "      <th>低层</th>\n",
       "      <th>高层</th>\n",
       "      <th>中层</th>\n",
       "      <th>...</th>\n",
       "      <th>高层</th>\n",
       "      <th>中层</th>\n",
       "      <th>低层</th>\n",
       "      <th>高层</th>\n",
       "      <th>中层</th>\n",
       "      <th>低层</th>\n",
       "      <th>高层</th>\n",
       "      <th>中层</th>\n",
       "      <th>低层</th>\n",
       "      <th>高层</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>装修</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>中装修</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>简装修</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>690.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>410.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>400.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>270.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>精装修</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>560.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>440.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>359.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>豪华装修</th>\n",
       "      <td>0.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>298.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "张贴日期 西元2017年4月12日             西元2017年4月13日           西元2017年4月14日              \\\n",
       "楼层             中层     低层   高层           中层   低层   高层           中层   低层     高层   \n",
       "装修                                                                              \n",
       "中装修           NaN    0.0  NaN          NaN  0.0  NaN          NaN  0.0    NaN   \n",
       "简装修           0.0    0.0  0.0          0.0  0.0  0.0          0.0  0.0    0.0   \n",
       "精装修           0.0    0.0  0.0         76.0  0.0  0.0        560.0  0.0  440.0   \n",
       "豪华装修          0.0  760.0  0.0          0.0  0.0  0.0          0.0  0.0  300.0   \n",
       "\n",
       "张贴日期 西元2017年4月15日 ...  西元2017年4月18日 西元2017年4月19日               西元2017年4月20日  \\\n",
       "楼层             中层 ...            高层           中层     低层     高层           中层   \n",
       "装修                ...                                                         \n",
       "中装修           NaN ...           NaN          NaN    0.0    NaN          NaN   \n",
       "简装修         690.0 ...           0.0          0.0  410.0    0.0        400.0   \n",
       "精装修           0.0 ...           0.0          0.0    0.0  359.0          0.0   \n",
       "豪华装修          0.0 ...           0.0        298.0    0.0    0.0          0.0   \n",
       "\n",
       "张贴日期           西元2017年4月21日            \n",
       "楼层     低层   高层           中层   低层   高层  \n",
       "装修                                     \n",
       "中装修   0.0  NaN          NaN  0.0  NaN  \n",
       "简装修   0.0  0.0        270.0  0.0  0.0  \n",
       "精装修   0.0  0.0          0.0  0.0  0.0  \n",
       "豪华装修  0.0  0.0          0.0  0.0  0.0  \n",
       "\n",
       "[4 rows x 30 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_wide = df_multi_idx.unstack()\n",
    "df_wide.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>张贴日期</th>\n",
       "      <th>西元2017年4月12日</th>\n",
       "      <th>西元2017年4月13日</th>\n",
       "      <th>西元2017年4月14日</th>\n",
       "      <th>西元2017年4月15日</th>\n",
       "      <th>西元2017年4月16日</th>\n",
       "      <th>西元2017年4月17日</th>\n",
       "      <th>西元2017年4月18日</th>\n",
       "      <th>西元2017年4月19日</th>\n",
       "      <th>西元2017年4月20日</th>\n",
       "      <th>西元2017年4月21日</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>装修</th>\n",
       "      <th>楼层</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>中装修</th>\n",
       "      <th>低层</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>285.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">简装修</th>\n",
       "      <th>中层</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>690.0</td>\n",
       "      <td>770.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>850.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>400.0</td>\n",
       "      <td>270.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>低层</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>355.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>410.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>高层</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>285.0</td>\n",
       "      <td>340.0</td>\n",
       "      <td>220.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>精装修</th>\n",
       "      <th>中层</th>\n",
       "      <td>0.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>560.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>295.0</td>\n",
       "      <td>926.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "张贴日期    西元2017年4月12日  西元2017年4月13日  西元2017年4月14日  西元2017年4月15日  西元2017年4月16日  \\\n",
       "装修  楼层                                                                         \n",
       "中装修 低层           0.0           0.0           0.0           0.0         285.0   \n",
       "简装修 中层           0.0           0.0           0.0         690.0         770.0   \n",
       "    低层           0.0           0.0           0.0           0.0           0.0   \n",
       "    高层           0.0           0.0           0.0         285.0         340.0   \n",
       "精装修 中层           0.0          76.0         560.0           0.0         200.0   \n",
       "\n",
       "张贴日期    西元2017年4月17日  西元2017年4月18日  西元2017年4月19日  西元2017年4月20日  西元2017年4月21日  \n",
       "装修  楼层                                                                        \n",
       "中装修 低层           0.0           0.0           0.0           0.0           0.0  \n",
       "简装修 中层           0.0         850.0           0.0         400.0         270.0  \n",
       "    低层         355.0           0.0         410.0           0.0           0.0  \n",
       "    高层         220.0           0.0           0.0           0.0           0.0  \n",
       "精装修 中层         295.0         926.0           0.0           0.0           0.0  "
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_long = df_wide.stack()\n",
    "df_long.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
